// pages/Albums/Albums.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        albums:[],
        arealist:[
            {
                name:'全部',
                area:'ALL'
            },
            {
                name:'华语',
                area:'ZH'
            },
            {
                name:'欧美',
                area:'EA'
            },
            {
                name:'日本',
                area:'JP'
            },
            {
                name:'韩国',
                area:'KR'
            },
        ],
        areaNum:0,
        area:'ALL',
        limit:15,
        offset:1,
        isLoading:false
    },
    // 改变语种选择
    changeArea(e){
        if(this.data.areaNum == e.currentTarget.dataset.index) return
        this.setData({
            areaNum:e.currentTarget.dataset.index,
            area:this.data.arealist[e.currentTarget.dataset.index].area
        }) 
        this.setData({
            offset:0,
            artistlist:[]
        })
        this.setData({
            offset:0,
            limit:15,
            albums:[]
        })
        this.getAlbums()
    },
    // 获取最新专辑
    async getAlbums(){
        wx.showLoading({
            title: 'loading...',
          })
        this.setData({
            isLoading:true
        })
        const res = await wx.p.request({
            url: `${wx.root}/top/album`,
            data:{
                limit:this.data.limit,
                offset:this.data.offset*this.data.limit,
                area:this.data.area
            }
        })
        const x = res.data.monthData.slice(this.data.offset*this.data.limit,(this.data.offset+1)*this.data.limit)
        this.setData({
            albums:[...this.data.albums,...x],
            offset:this.data.offset+1,
            limit:30
        })
        wx.hideLoading({
          success: (res) => {
              this.setData({
                  isLoading:false
              })
          },
        })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.getAlbums()
    },


    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        if(this.data.isLoading == false)
        this.getAlbums()
    },
})